﻿Imports CRM.BusinessLayer
Imports System.Data

Partial Public Class frmHistory
    Public blnLoadMainform As Boolean = False
    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClear.Click
        Dim dt As DataTable = Nothing
        Try
            If GlobalCRM.CurrentConViewID = GlobalCRM.CurrentConID Then
                If MsgBox("Are you sure?", MsgBoxStyle.YesNo, "Clear History") = MsgBoxResult.Yes Then
                    If CommonDataFunctions.DeleteHistory(GlobalCRM.CurrentConID) = True Then
                        'DataBinding.BindListView(Me.HistorySQL, Me.lvwHistory)
                        dt = CommonDataFunctions.HistoryData(GlobalCRM.CurrentConID)
                        DataBinding.BindListView(dt, Me.lvwHistory)
                    End If
                End If
            Else
                CommonFuncs.DisplayMessage("You are not authorized to clear others history data.", CommonDialog.CustomDialogIcons.Information, "Clear History")
                
            End If
        Catch ex As Exception
        Finally
            dt = Nothing
        End Try
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnClose.Click
        blnLoadMainform = False
        Me.Close()
    End Sub

    Private Sub frmHistory_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles Me.KeyUp
        If e.Key = Key.Escape Then
            blnLoadMainform = False
            Me.Close()
        End If

    End Sub

    Private Sub frmHistory_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        GeneralTasks.ChangeTheme(Me)
        GeneralTasks.EnumerateandFill(Me.LogicalChildren)
        Dim dt As DataTable = Nothing
        Try
            dt = CommonDataFunctions.HistoryData(GlobalCRM.CurrentConViewID)
            DataBinding.BindListView(dt, Me.lvwHistory)
        Catch ex As Exception
        Finally
            dt = Nothing
        End Try
    End Sub

    Private Sub lvwHistory_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lvwHistory.MouseDoubleClick
        GeneralTasks.ChangeTheme(Me)
        If lvwHistory.SelectedItems.Count > 0 Then
            GlobalCRM.ResetGlobalVars()
            'Dim frmMainDisplay As New frmCRMMainDisplay
            If lvwHistory.SelectedItem.row.itemarray(2).ToString = "Case" Then

                GlobalCRM.CurrentCaseID = lvwHistory.SelectedItem.row.itemarray(3).ToString

                'See if a corporate case and set companyid as well

                CommonDataFunctions.GblFunc_Goto_ID(GlobalCRM.CurrentCaseID, CommonDataFunctions.DBIdTypes.CaseID)
                'MainDisplay.ActiveTab = MainDisplay.PageTabs.Cases

            ElseIf lvwHistory.SelectedItem.row.itemarray(2).ToString = "Company" Then
                'GlobalCRM.CurrentCaseID = 0
                GlobalCRM.CurrentCompanyID = lvwHistory.SelectedItem.row.itemarray(3).ToString
                CommonDataFunctions.GblFunc_Goto_ID(GlobalCRM.CurrentCompanyID, CommonDataFunctions.DBIdTypes.CompanyID)

            ElseIf lvwHistory.SelectedItem.row.itemarray(2).ToString = "Person" Then
                'GlobalCRM.CurrentCaseID = 0
                GlobalCRM.CurrentCandidateID = lvwHistory.SelectedItem.row.itemarray(3).ToString

                CommonDataFunctions.GblFunc_Goto_ID(GlobalCRM.CurrentCandidateID, CommonDataFunctions.DBIdTypes.PersonID)

                'frmMainDisplay.ActiveTab = frmCRMMainDisplay.PageTabs.Person
            End If
            frmCRMMainDisplay.loadTabCompany = False
            frmCRMMainDisplay.loadTabCases = False
            frmCRMMainDisplay.loadTabAccounts = False
            frmCRMMainDisplay.loadTabDocs = False
            frmCRMMainDisplay.loadTabPeople = False
            frmCRMMainDisplay.loadTabEvent = False
            blnLoadMainform = True
            Me.Close()

            'frmMainDisplay.MdiParent = frmMDIMain
        End If
    End Sub
End Class
